package cn.buk.qms.web;

import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.dto.UserDto;
import cn.buk.tms.entity.misc.User;
import cn.buk.qms.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author yfdai
 * @date 2017/1/7
 */
@RestController
public class UsersController extends BaseController {

  private static final Logger logger = LogManager.getLogger(UsersController.class);

  @Value("${owner_enterprise_id}")
  private int ownerId;

  @Override
  @InitBinder("sc")
  public void initBinder1(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("sc.");
  }

  @Autowired
  private UserService userService;


  /**
   * 搜索员工信息
   */
  @GetMapping("/dept/{deptId}/users")
  public CommonDto<UserDto> searchEmployees(HttpServletRequest request,
                                            @PathVariable("deptId") final int deptId,
                                            @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                            @RequestParam(value = "pageSize", defaultValue = "25") final int pageSize
  ) {
    final int enterpriseId = getEnterpriseId(request);

    CommonDto<UserDto> commonDto = new CommonDto<>();

    CommonSearchCriteria sc = new CommonSearchCriteria();


    List<User> users = userService.searchUsersByDepartment(enterpriseId, deptId, sc);

    List<UserDto> dtos = new ArrayList<>();
    for (User user : users) {
      UserDto dto = new UserDto();
      dtos.add(dto);

      dto.setId(user.getId());
      dto.setEnterpriseId(user.getEnterprise().getId());
      dto.setUsername(user.getUsername());
      dto.setEmpname(user.getEmpname());

      dto.setWwUserId(user.getWwUserId());

      dto.setEmail(user.getEmail());
      dto.setMobileNo(user.getMobileNo());
      dto.setStatus(user.getStatus());
    }

    commonDto.setDataList(dtos);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

}
